﻿using Infrastructure.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zj.OpenAuth.App.RoleApp;
using Zj.OpenAuth.DTO.Common;
using Zj.OpenAuth.Mvc.Models;

namespace Zj.OpenAuth.Mvc.Controllers
{
    public class RolesController : Controller
    {
        private IRoleAppService _service;

        public RolesController(IRoleAppService service)
        {
            this._service = service;
        }



        // GET: Roles
        public ActionResult Index()
        {
            return View();
        }




        public ActionResult GetAllPageList(int startPage, int pageSize)
        {
            int rowCount = 0;
            var result = _service.GetAllPageList(startPage, out rowCount, pageSize);
            return Json(new
            {
                rowCount = rowCount,
                pageCount = Math.Ceiling(Convert.ToDecimal(rowCount) / pageSize),
                rows = result,
            }, JsonRequestBehavior.AllowGet);
        }


        public ActionResult GetMenusByRole(int? roleId)
        {
            return Json(_service.GetAllMenuListByRole(roleId.Value).Select(r=>r.ToString()), JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetRolesByUser(int? userId)
        {
            return Json(_service.GetAllRoleListByUser(userId.Value).Select(r => r.ToString()), JsonRequestBehavior.AllowGet);
        }

        public ActionResult Get(int? id)
        {
            return Json(_service.Get(id.Value), JsonRequestBehavior.AllowGet);
        }


        public ActionResult GetAll()
        {
            return Json(_service.GetAllList(), JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Save(RoleDTO dto)
        {
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult(DoResult.Failed, "保存失败"), JsonRequestBehavior.AllowGet);
            }
            if (_service.AddOrEdit(dto))
            {
                return Json(new AjaxResult(DoResult.Success, "保存成功"), JsonRequestBehavior.AllowGet);
            }
            return Json(new AjaxResult(DoResult.Failed, "保存失败"), JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public ActionResult SavePermission(int roleId,List<RoleMenuMappingDTO> roleMenus)
        {
            if (_service.UpdateRoleMenu(roleId, roleMenus))
            {
                return Json(new AjaxResult(DoResult.Success, "保存成功"), JsonRequestBehavior.AllowGet);
            }
            return Json(new AjaxResult(DoResult.Failed, "保存失败"), JsonRequestBehavior.AllowGet);
        }

        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return Json(new AjaxResult(DoResult.Failed, "删除失败"), JsonRequestBehavior.AllowGet);
            }
            _service.Delete(id.Value);
            return Json(new AjaxResult(DoResult.Success, "删除成功"), JsonRequestBehavior.AllowGet);
        }



        public ActionResult DeleteMuti(string ids)
        {
            var idList = ids.Split(',').Select(s => Convert.ToInt32(s)).ToList();
            _service.DeleteMuti(idList);
            return Json(new AjaxResult(DoResult.Success, "删除成功"), JsonRequestBehavior.AllowGet);
        }
    }
}